<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:p="http://primefaces.org/ui"
                template="./../../template/tagedu.xhtml">

    <ui:define name="title">
        <title><h:outputText value="Planejamento - TAG"/></title>
    </ui:define>

    <ui:define name="content">
        <h:outputText styleClass="title" value="Planejamento"/>
        
        <h:form id="editWorksForm">
            <p:messages id="message" showDetail="true" />
            
            <h:panelGrid id="loadForm" styleClass="load" columns="2">
                <h:outputText value="Organização: "/>
                <p:selectOneMenu value="#{workMB.organization}" converter="orgC">
                    <f:selectItems value="#{workMB.organizations}" var="org" itemLabel="#{org.name}" itemValue="#{org}"/>
                </p:selectOneMenu>
                <h:outputText value="Discipline: "/>
                <p:selectOneMenu value="#{workMB.discipline}" converter="disciplineC">
                    <f:selectItems value="#{workMB.disciplinePerOrganization}" var="d" itemLabel="#{d.name}" itemValue="#{d}"/>
                </p:selectOneMenu>
                <h:outputText value="Ano: "/>
                <p:spinner value="#{workMB.workYear}" min="#{workMB.actualYear-1}" max="#{workMB.actualYear+1}" size="4">
                    <f:validator validatorId="yearV"/>
                    <f:attribute name="minimo" value="#{(workMB.actualYear-1).toString()}"/>
                    <f:attribute name="maximo" value="#{(workMB.actualYear+1).toString()}"/>
                </p:spinner>
                <p:commandButton id="load" value="Carregar" action="#{workMB.loadNodes()}"
                        update="tree,message" ajax="true" process="@parent"/>
            </h:panelGrid>
            
            <p:contextMenu for="treeWorks">
                <p:menuitem value="Criar subtópico" oncomplete="createNode.show()"
                            actionListener="#{workMB.prepareNode()}" icon="ui-icon-newwin"/>
                <p:menuitem value="Renomear" onclick="renameNode.show()" icon="ui-icon-tag"/>
                <p:menuitem value="Desabilitar" update=":disableNode" oncomplete="disableNodeW.show()" icon="ui-icon-close"/>
            </p:contextMenu>
            
            <p:separator />
            
            <h:panelGroup id="tree" layout="block">
                <p:tree id="treeWorks" value="#{workMB.root}" var="node" style="width: 800px;"
                        selectionMode="single" selection="#{workMB.nodeSelected}">
                    <p:treeNode icon="ui-icon-clipboard">
                        <h:outputText value="#{node.code} - " rendered="#{node.code != null}"/>
                        <h:outputText value="#{node.name}"/>
                    </p:treeNode>
                </p:tree>
                <p:separator />
                <p:commandButton value="Criar tópico" oncomplete="createSuperNode.show()" disabled="#{workMB.root == null}"
                                 actionListener="#{workMB.prepareNode()}" icon="ui-icon-newwin"/>&nbsp;&nbsp;
                <p:commandButton value="Download PDF" disabled="#{workMB.root == null}" ajax="false"
                                 action="#{workMB.generatePDF()}" icon="ui-icon-print"/>&nbsp;&nbsp;
                <p:commandButton action="#{workMB.generateCodes()}" value="Gerar códigos"
                                 disabled="#{workMB.root == null}" icon="ui-icon-flag"/>
            </h:panelGroup>
            
        </h:form>
    </ui:define>

    <ui:define name="modals">
        <p:dialog modal="true" closable="false" header="Novo tópico" resizable="false" widgetVar="createSuperNode">
            <h:form>
                <h:panelGrid columns="2">
                    <h:outputText value="Nome: "/>
                    <p:inputText required="true" value="#{workMB.topic.name}"/>
                    <h:outputText value="Ano inicial: " title="primeiro ano em que o tópico é válido"/>
                    <p:spinner value="#{workMB.manageYear}" min="#{workMB.actualYear}" max="#{workMB.actualYear + 1}">
                        <f:validator validatorId="yearV"/>
                        <f:attribute name="minimo" value="#{(workMB.actualYear).toString()}"/>
                        <f:attribute name="maximo" value="#{(workMB.actualYear+1).toString()}"/>
                    </p:spinner>
                    <p:commandButton value="Salvar" action="#{workMB.createSuperNode()}"
                        update=":editWorksForm:message,:editWorksForm:treeWorks" onsuccess="createSuperNode.hide()"/>
                    <p:commandButton value="Cancelar" action="#{workMB.clear()}" onsuccess="createSuperNode.hide()"/>
                </h:panelGrid>
            </h:form>
        </p:dialog>
        <p:dialog modal="true" closable="false" header="Novo subtópico" resizable="false" widgetVar="createNode">
            <h:form>
                <h:panelGrid columns="2">
                    <h:outputText value="Nome: "/>
                    <p:inputText required="true" value="#{workMB.topic.name}"/>
                    <h:outputText value="Ano inicial: " title="primeiro ano em que o subtópico é válido"/>
                    <p:spinner value="#{workMB.manageYear}" min="#{workMB.actualYear}" max="#{workMB.actualYear + 1}">
                        <f:validator validatorId="yearV"/>
                        <f:attribute name="minimo" value="#{(workMB.actualYear).toString()}"/>
                        <f:attribute name="maximo" value="#{(workMB.actualYear+1).toString()}"/>
                    </p:spinner>
                    <p:commandButton value="Salvar" action="#{workMB.createNote()}"
                        update=":editWorksForm:message,:editWorksForm:treeWorks" onsuccess="createNode.hide()"/>
                    <p:commandButton value="Cancelar" action="#{workMB.clear()}" onsuccess="createNode.hide()"/>
                </h:panelGrid>
            </h:form>
        </p:dialog>
        <p:dialog modal="true" closable="false" header="Renomear" resizable="false" widgetVar="renameNode">
            <h:form>
                <h:panelGrid columns="2">
                    <h:outputText value="Nome: "/>
                    <p:inputText required="true" value="#{workMB.nodeSelected.data.name}"/>
                    <h:outputText value="Ano final: " title="ultimo ano em que o tópico é válido com nome antigo"/>
                    <p:spinner value="#{workMB.manageYear}" min="#{workMB.actualYear}" max="#{workMB.actualYear + 1}">
                        <f:validator validatorId="yearV"/>
                        <f:attribute name="minimo" value="#{(workMB.actualYear).toString()}"/>
                        <f:attribute name="maximo" value="#{(workMB.actualYear+1).toString()}"/>
                    </p:spinner>
                    <p:commandButton value="Salvar" action="#{workMB.renameNode()}"
                        update=":editWorksForm:message,:editWorksForm:treeWorks" onsuccess="renameNode.hide()"/>
                    <p:commandButton value="Cancelar" onclick="renameNode.hide()"/>
                </h:panelGrid>
            </h:form>
        </p:dialog>
        <p:dialog id="disableNode" modal="true" closable="false" resizable="false" widgetVar="disableNodeW"
                header="Desabilitar tópico #{workMB.nodeSelected.data.name}" >
            <h:form>
                <h:panelGrid columns="2">
                    <h:outputText value="Ano final: " title="ultimo ano em que o tópico #{workMB.nodeSelected.data.name} é válido"/>
                    <p:spinner value="#{workMB.manageYear}" min="#{workMB.actualYear}" max="#{workMB.actualYear+1}">
                        <f:validator validatorId="yearV"/>
                        <f:attribute name="minimo" value="#{(workMB.actualYear).toString()}"/>
                        <f:attribute name="maximo" value="#{(workMB.actualYear+1).toString()}"/>
                    </p:spinner>
                </h:panelGrid>
                <p:separator/>
                <p:commandButton value="Desabilitar" action="#{workMB.disableNode}"
                    onsuccess="disableNodeW.hide()" update=":editWorksForm:message,:editWorksForm:treeWorks"/>
                <p:commandButton value="Remover" rendered="#{workMB.canRemove()}"
                    onclick="disableNodeW.hide(); deleteNodeW.show();"/>
                <p:commandButton value="Cancelar" onclick="disableNodeW.hide()"/>
            </h:form>
        </p:dialog>
        <p:confirmDialog id="deleteNode" message="Antes de remover, repense se não seria melhor desabilita-lo."  
                header="Operação crítica" severity="alert" widgetVar="deleteNodeW">
            <h:form>
                <p:commandButton id="confirm" value="Remover" update=":editWorksForm:message,:editWorksForm:treeWorks"
                                 oncomplete="deleteNodeW.hide()" actionListener="#{workMB.deleteNode()}"/>
                <p:commandButton id="decline" value="Cancelar" onclick="deleteNodeW.hide()" type="button"/>
            </h:form>
        </p:confirmDialog>
    </ui:define>
</ui:composition>